//
//  AppDelegate+DTCRootController.m
//  BluetoothProject
//
//  Created by Kendama Chen on 17/8/16.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "AppDelegate+DTCRootController.h"
#import "TabBarViewController.h"
#import "LoginViewController.h"

@interface AppDelegate ()

@end



@implementation AppDelegate (DTCRootController)

- (void)setRootViewController
{
    if ([EXRACT_OBJECT(LOGIN) isEqualToString:@"YES"])
    {
        TabBarViewController *tabVC = [TabBarViewController new];
        self.window.rootViewController = tabVC;
        self.mainController = tabVC;
    }
    else
    {
        LoginViewController *vc = [LoginViewController new];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        self.window.rootViewController = nav;
    }

    [self.window makeKeyAndVisible];


}

- (void)signOut
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        EMError *error = [[EMClient sharedClient] logout:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            if (error != nil)
            {
                [SVProgressHUD showErrorWithStatus:error.errorDescription];
            }
            else
            {
                SAVE_OBJECT(@"NO", LOGIN);
                REMOVE_OBJECT(DEVICEID);
                REMOVE_OBJECT(GID);
                [self setRootViewController];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
            }
        });
    });

}

- (void)setTabbar
{
    TabBarViewController *tabVC = [TabBarViewController new];
    self.window.rootViewController = tabVC;
    self.mainController = tabVC;
    [self.window makeKeyAndVisible];
}


@end
